package vn.edu.saigontech.ncms.util;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;

public class I18n {
	
	public static final String KEY = "i18n";
	public static final String I18N_FILE_NAME = "messages";
	
	private ResourceBundle resourceBundle;
	
	public I18n() {
		this(Global.INSTANCE.getValue(Global.KEY_LANGUAGE));
	}
	
	public I18n(String language) {
		this.resourceBundle = ResourceBundle.getBundle(I18N_FILE_NAME, new Locale(language));
	}
	
	public String getMessage(String key) {
		return this.getMessage(key, new Object[] {});
	}
	
	public String getMessage(String key, Object... params) {
		try {
			return new MessageFormat(this.resourceBundle.getString(key)).format(params);
		}
		catch(Exception ex) {
			return key;
		}
	}
	
	public String getLanguage() {
		return this.resourceBundle.getLocale().getLanguage(); 
	}
	
	public ResourceBundle getResourceBundle() {
		return this.resourceBundle;
	}
}